package com.adcc.zkdemo.web;

import com.adcc.zkdemo.core.Airport;
import com.adcc.zkdemo.core.AirportService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zkplus.spring.DelegatingVariableResolver;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.*;
import org.zkoss.zul.ext.Paginal;

/**
 * Created by liup on 14-12-25.
 */
public class DataTable extends Borderlayout  implements EventListener<Event> {

    Label labTitle;

    Label lab3code;
    Label lab4code;

    Textbox txt3code;
    Textbox txt4code;

    Button btnSave;
    Button btnDel;

    Grid dataList;
    Grid form;

    String id;

    Paging pag;

    Label labCode3Search;
    Textbox txtCode3Search;
    Button btnSearch;

    private AirportService airportService;

    public DataTable() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
        this.createLayout();
    }

    private void createLayout() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        airportService = (AirportService)SpringUtil.getBean("airportService");
        North titlePanel = new North();
        Center tablePanel = new Center();
        West formPanel = new West();

        titlePanel.setSize("30px");
        labTitle = new Label("DataDemo");
        labTitle.setStyle("font-size:1.15em;font-weight:bold;font-family:Microsoft YaHei");

        dataList = new Grid();
        dataList.appendChild(new Columns());
        dataList.appendChild(new Rows());           //TODO:改为统一的获取Grid方法
        Auxhead dataListHead = new Auxhead();
        dataListHead.setAttribute("label","Airport DataList");
        Auxheader auxSearch = new Auxheader();
        auxSearch.setColspan(4);
        txt3code = new Textbox();
        lab3code = new Label("Search by Code3");
        btnSearch = new Button("Search");
        auxSearch.appendChild(lab3code);
        auxSearch.appendChild(txt3code);
        auxSearch.appendChild(btnSearch);
        dataListHead.appendChild(auxSearch);

        Column colId = new Column("id");
        colId.setSort("auto");
        Column col3code = new Column("3Code");
        col3code.setSort("auto");
        Column col4code = new Column("4Code");
        col4code.setSort("auto");
        Column colEdit = new Column("Edit");
        //dataList.appendChild(dataListHead);
        dataList.getColumns().setMenupopup("auto");
        dataList.getColumns().appendChild(colId);
        dataList.getColumns().appendChild(col3code);
        dataList.getColumns().appendChild(col4code);
        dataList.getColumns().appendChild(colEdit);
        pag = new Paging();
        pag.setPageSize(10);
        pag.setTotalSize(airportService.getCount());
        //pag.setId("paging");
        dataList.setPaginal(pag);
        //dataList.setMold("paging");
        //dataList.setMold("paging");
        //dataList.getHeads().add(dataListHead);
        //dataList.setPageSize(10);
        dataList.insertBefore(dataListHead,dataList.getColumns());
        //dataList.insertBefore(pag,dataListHead);
        formPanel.setWidth("500px");

        form = new Grid();
        form.appendChild(new Columns());
        form.appendChild(new Rows());
        Column colType = new Column("Type");
        Column colContent = new Column("Content");
        form.getColumns().appendChild(colType);
        form.getColumns().appendChild(colContent);

        lab3code = new Label("3Code");
        txt3code = new Textbox();
        txt3code.setWidth("150px");
        txt3code.setTabindex(1);
        Hlayout hlay3code = new Hlayout();
        hlay3code.appendChild(txt3code);
        Row row3code = new Row();
        row3code.appendChild(lab3code);
        row3code.appendChild(hlay3code);
        form.getRows().appendChild(row3code);

        lab4code = new Label("4Code");
        txt4code = new Textbox();
        txt4code.setWidth("150px");
        txt4code.setTabindex(2);
        Hlayout hlay4code = new Hlayout();
        hlay4code.appendChild(txt4code);
        Row row4code = new Row();
        row4code.appendChild(lab4code);
        row4code.appendChild(hlay4code);
        form.getRows().appendChild(row4code);

        Cell cell = new Cell();
        cell.setAttribute("colspan","3");
        cell.setStyle("text-align:center");
        Hlayout hlaySave = new Hlayout();
        btnSave = new Button("Save");
        btnSave.setId("btnSave");
        btnSave.addEventListener("onClick",this);
        btnDel = new Button("Del");
        btnDel.setId("btnDel");
        btnDel.addEventListener("onClick",this);
        hlaySave.appendChild(btnSave);
        hlaySave.appendChild(btnDel);
        Row rowSave = new Row();
        rowSave.appendChild(cell);
        rowSave.appendChild(hlaySave);
        form.getRows().appendChild(rowSave);

        //tablePanel.appendChild(dataList);
        Window w = new Window();
        w.appendChild(dataList);
        w.appendChild(pag);
        tablePanel.appendChild(w);
        titlePanel.appendChild(labTitle);
        formPanel.appendChild(form);
        this.appendChild(titlePanel);
        this.appendChild(tablePanel);
        this.appendChild(formPanel);

        this.loadData();
    }

    public void loadData()
    {
        this.dataList.removeChild(this.dataList.getRows());
        Rows rows = new Rows();
        for(Airport airport : airportService.getAll())
        {
            Label _labId = new Label(airport.getId().toString());
            Label _lab3code = new Label(airport.getCode3());
            Label _lab4code = new Label(airport.getCode4());
            Button _btnEdit = new Button("Edit");
            _btnEdit.setId("btnEdit@" + airport.getId().toString());
            _btnEdit.addEventListener("onClick",this);
            Row r = new Row();
            r.appendChild(_labId);
            r.appendChild(_lab3code);
            r.appendChild(_lab4code);
            r.appendChild(_btnEdit);
            rows.appendChild(r);
        }
        this.dataList.appendChild(rows);
    }

    public void clean()
    {
        this.id = null;
        txt3code.setValue("");
        txt4code.setValue("");
    }

    @Override
    public void onEvent(Event event) throws Exception {
        if (event.getName().equals(Events.ON_CLICK))
        {
            Component comp = event.getTarget();
            if(comp instanceof Button && comp.getId().equals("btnSave"))
            {
                if(txt3code.getValue() != "" && txt4code.getValue() != "")
                {
                    if(this.id != null && !this.id.equals(""))
                    {
                        airportService.Updata(this.id,txt3code.getValue(),txt4code.getValue());
                    }  else {
                        Airport saveEntity = new Airport();
                        saveEntity.setCode3(txt3code.getValue());
                        saveEntity.setCode4(txt4code.getValue());
                        airportService.add(saveEntity);
                    }
                    this.loadData();
                    this.clean();
                    Messagebox.show("Add Finish!");
                }else
                {
                    Messagebox.show("code3 and code4 cannot be null!");
                }
            }
            if(comp instanceof Button && comp.getId().equals("btnDel"))
            {
                if(this.id != null && !this.id.equals(""))
                {
                    airportService.Del(this.id);
                    this.loadData();
                    this.clean();
                    Messagebox.show("Del Finish");
                }
            }
            if(comp instanceof Button && comp.getId().indexOf("btnEdit") >= 0)
            {
                Airport finded = airportService.findById(comp.getId().split("@")[1]);
                this.id = finded.getId().toString();
                this.txt3code.setValue(finded.getCode3());
                this.txt4code.setValue(finded.getCode4());
            }
        }
    }
}
